<?php

namespace app\controllers\user;

use app\common\base\CommonController;

/**
 * 管理员用户的相关用户操作
 * * 添加、删除用户
 * * 添加、删除群组内用户
 */

class AdminController extends CommonController
{
    public function actionListUsers()
    {
        $info = parent::callModuleService('user', 'AdminService', 'listUsers', $this->userInfo->id);
        return $info;
    }
    /**
     * 创建用户
     */
    public function actionCreateUser()
    {
        $this->rules = [
            [['username', 'password'], 'required'],
            [['username', 'password'], 'string'],
        ];
        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('user', 'AdminService', 'createUser', $this->userInfo->id, $inputs['username'],$inputs['password']);

        return [
            'success' => $info['success'],
        ];
    }

    /**
     * 删除用户
     */
    public function actionDeleteUser()
    {
        $this->rules = [
            [['username'], 'required'],
            [['username'], 'string'],
        ];

        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info=parent::callModuleService('user', 'AdminService', 'deleteUser', $inputs['username']);
        
        return [
            'success' => $info['success'],
        ];
    }

    /**
     * 创建管理员（预留接口）
     */
    public function actionCreateAdmin()
    {
        $this->rules = [
            [['username', 'password'], 'required'],
            [['username', 'password'], 'string'],
        ];
        $inputs = $this->validate();
        $info=parent::callModuleService('user', 'AdminService', 'createAdmin', $inputs['username'], $inputs['password']);
        return [
            'success' => $info['success'],
        ];
    }
    
    /**
     * 删除管理员（预留接口）
     */
    public function actionDeleteAdmin()
    {
        $this->rules = [
            [['username'], 'required'],
            [['username'], 'string'],
        ];
        $inputs = $this->validate();
        $info=parent::callModuleService('user', 'AdminService', 'deleteUser', $inputs['username']);
        return [
            'success' => $info['success'],
        ];
   }

   /**
    * 创建操作
    */
   public function actionAddOperation()
   {
        $this->rules = [
            [['operationName'],'required'],
            [['operationName'],'string'],
        ];
        $inputs = $this->validate();
        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }
        $info = parent::callModuleService('log', 'OperationService', 'add', $inputs['operationName']);
        return [
            'info' => $info,
        ];
   }

   /**
    * 删除操作
    */
    public function actionDeleteOperation()
    {
        $this->rules = [
            [['operationName'],'string'],
            [['operationId'],'integer'],
        ];
        $inputs = $this->validate();
        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }
        if (($inputs['operationName']=='NULL')&&($inputs['operationId']==0))
        return parent::error('Error_');/*想不出错误名称啦 */
        if ($inputs['operationId']==0)
        $inputs['operationId']=parent::callModuleService('log', 'OperationService', 'findId', $inputs['operationName']);
        $info = $info = parent::callModuleService('log', 'OperationService', 'delete', $inputs['operationId']);
        return [
            'success'=>$info['success'],
        ];
    }
}
